home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 17841 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  4.0 KB

  1. Path: news.ahc.ameritech.com!datalytics!usenet
  2. From: Rob Stewart <stew@datalytics.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Constructor and enum Question...
  5. Date: Wed, 17 Apr 1996 15:33:40 -0400
  6. Organization: Datalytics, Inc
  7. Message-ID: <31754794.1039@datalytics.com>
  8. References: <4kujav$892@news.nde.state.ne.us> <3173D2C2.4D68@demos.su>
  9. NNTP-Posting-Host: 204.62.224.71
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0 (WinNT; I)
  14.  
  15. Alexey Ruzin wrote:
  16. > cwu wrote:
  17. > >
  18. > > Recently I am studying C++ and have a difficulty to understand
  19. > > "constructor" and "enum".  I am very appreciated any comments.
  20. > Hi, excuse me for my Engilsh.
  21. > As for 'enum':
  22. > In short, it looks like set of defines, which declare 'int' constants.
  23. > These defines are grouped in type, but you don't need use it mnemonic
  24. > names always. Example:
  25. > ...
  26. > enum {a,b,c} f;
  27. > ...
  28. > this is equal:
  29. > ...
  30. > #define a 0
  31. > #define b 1
  32. > #define c 2
  33.  
  34. Well, not exactly.  It declares a new type, compatible with int, 
  35. that gives names to the values.  (#define creates a number which 
  36. takes on a different type depending upon the expression in which 
  37. the expanded value--a literal number--appears.) By default, the 
  38. enumerations (the named values in the enumerated type) start at 
  39. zero and increase sequentially.  You can, however, assign 
  40. arbitrary values to any of them.  The compiler will increment, 
  41. by one, any that don't have values along the way.
  42.  
  43. enum
  44. {
  45.    a = 5,        // 5
  46.    b,            // 6
  47.    c = 0        // 0
  48. };
  49.  
  50. The tag-name may be omitted, as above, if you only want to use 
  51. the enumerations and not declare a variable or parameter of the 
  52. enumerated type.  If you omit the tag-name, you are creating the 
  53. equivalent of a set of const ints.
  54.  
  55. With the tag-name, you can declare variables and parameters of 
  56. that enumerated type, so they can only assume values from the 
  57. enumerations in that type.  You cannot, however, assign a 
  58. variable of an enumerated type from anything other than the 
  59. available enumerations.
  60.  
  61. (Most of the preceding statements can be qualified if you start 
  62. casting!)
  63.  
  64. > As for 'constructor':
  65. > [snip]
  66.  
  67. A constructor (abbreviated ctor or c'tor) is a member function 
  68. of a class that is charged with turning raw memory--whether on 
  69. the stack or heap--into an object.  Typically, this 
  70. transformation involves initializing data members (dms).  Prior 
  71. to entering the body of the ctor, while processing the 
  72. initializer list, if any, the compiler inserts code that calls 
  73. base class and dm ctors.  Thus, by the time you enter the ctor 
  74. body (after the opening brace), all base classes and dms have 
  75. been initialized in some fashion.  From this point forward, a 
  76. ctor is like any other member function.  You can put any 
  77. initialization code into it you like.
  78.  
  79. ctors are like any other member function (mf) except that ctors 
  80. have no return value (not even void), and they (only one per 
  81. object) are called before any other mf.  What makes ctors 
  82. special is that they share the class name.  This is how the 
  83. compiler recognizes a particular member function as a ctor.
  84.  
  85. Like other mfs, ctors may be overloaded, so each may take 
  86. different parameters.  In fact, if you don't create a ctor for 
  87. your class, the compiler will create two for you: the default 
  88. and copy ctors.  The former takes no parameters and simply 
  89. invokes base class and dm default ctors.  The latter does a 
  90. shallow (duplicates pointers rather than what they point to) 
  91. copy of one object to the new one.
  92.  
  93. BTW, ctors are matched by a destructor (dtor) (only one per 
  94. class).  The dtor is responsible for reverting the object back 
  95. into raw memory.  This can mean releasing resources (like memory 
  96. allocated on the heap), or notifying cooperating objects that 
  97. they are disappearing, etc.
  98.  
  99. It comes down to this: a ctor is the first member function 
  100. called on raw memory to turn it into an object, and the dtor is 
  101. the last member function called on an object to turn it back 
  102. into raw memory.
  103.  
  104. -- 
  105. Robert Stewart        | My opinions are usually my own.
  106. Datalytics, Inc.    | stew@datalytics.com
  107.